<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="headerr">
        <span>用户管理</span>
        <div class="inheader">
          <div>
            <el-input
              placeholder="请输入内容"
              v-model="userobj.query"
              class="input-with-select"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="shuser"
              ></el-button>
            </el-input>
          </div>
          <div>
            <el-button type="primary" @click="dialogVisible = true"
              >添加</el-button
            >
            <el-dialog
              title="添加用户"
              :visible.sync="dialogVisible"
              width="30%"
            >
              <div>
                <el-form
                  label-width="70px"
                  :model="addobj"
                  :rules="relus"
                  ref="ruleForm"
                >
                  <el-form-item label="用户名" prop="username">
                    <el-input v-model="addobj.username"></el-input>
                  </el-form-item>
                  <el-form-item label="密码" prop="password">
                    <el-input v-model="addobj.password"></el-input>
                  </el-form-item>
                  <el-form-item label="邮箱" prop="email">
                    <el-input v-model="addobj.email"></el-input>
                  </el-form-item>
                  <el-form-item label="手机号" prop="mobile">
                    <el-input v-model="addobj.mobile"></el-input>
                  </el-form-item>
                </el-form>
              </div>
              <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addu">确 定</el-button>
              </span>
            </el-dialog>
          </div>
        </div>
      </div>
      <div>
        <template>
          <el-table :data="queryobj" border style="width: 100%">
            <el-table-column prop="id" label="序号" width="180">
            </el-table-column>
            <el-table-column prop="username" label="用户名" width="180">
            </el-table-column>
            <el-table-column prop="email" label="邮箱地址"> </el-table-column>
            <el-table-column prop="mobile" label="电话"> </el-table-column>
            <el-table-column prop="role_name" label="角色"> </el-table-column>
            <el-table-column label="状态">
              <template slot-scope="{ row }">
                <el-switch
                  v-model="row.mg_state"
                  active-color="#1e90ff"
                  inactive-color="#a4b0be"
                  @change="statech(row)"
                >
                </el-switch>
              </template>
            </el-table-column>
            <el-table-column prop="address" label="操作">
              <template slot-scope="{ row }">
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  @click="bianjiquery(row)"
                ></el-button>

                <el-button icon="el-icon-setting" type="warning"></el-button>
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  @click="del(row)"
                ></el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pagenum"
            :page-sizes="[2, 4, 6, 8]"
            :page-size="100"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </template>
        <el-dialog title="提示" :visible.sync="dialogbj" width="30%">
          <el-form :model="bianjiobj" label-width="70px">
            <el-form-item label="序号">
              <el-input v-model="bianjiobj.id"></el-input>
            </el-form-item>

            <el-form-item label="邮箱地址">
              <el-input v-model="bianjiobj.email"></el-input>
            </el-form-item>
            <el-form-item label="电话">
              <el-input v-model="bianjiobj.mobile"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogbj = false">取 消</el-button>
            <el-button type="primary" @click="bianji">确 定</el-button>
          </span>
        </el-dialog>
      </div>
    </el-card>
  </div>
</template>

<script>
import {
  queryuser_api,
  addusert_api,
  state_api,
  search_api,
  bianji_api,
  del_api,
} from "../../api/users";
export default {
  data() {
    //验证用户名
    let validateusername = (rule, value, callback) => {
      if (value) {
        let reg = /^[0-9a-z]{3,15}$/gi;
        if (reg.test(value)) {
          callback();
        } else {
          callback(new Error("用户名格式不对！"));
        }
      } else {
        callback(new Error("请输入用户名！"));
      }
    };
    //验证密码;
    let validatepassword = (rule, value, callback) => {
      if (value) {
        let reg = /^[0-9a-z]{3,9}$/gi;
        if (reg.test(value)) {
          callback();
        } else {
          callback(new Error("密码格式不对！"));
        }
      } else {
        callback(new Error("请输入密码！"));
      }
    };
    //验证邮箱
    let validateemail = (rule, value, callback) => {
      if (value) {
        let reg =
          /^([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?$/i;
        if (reg.test(value)) {
          callback();
        } else {
          callback(new Error("邮箱格式不对！"));
        }
      } else {
        callback(new Error("请输入邮箱！"));
      }
    };
    //验证手机号
    let validatemobile = (rule, value, callback) => {
      if (value) {
        let reg = /^[0-9]{11}$/;
        if (reg.test(value)) {
          callback();
        } else {
          callback(new Error("手机号格式不对！"));
        }
      } else {
        callback(new Error("请输入手机号！"));
      }
    };
    return {
      //
      bianjiobj: {},
      //
      relus: {
        username: [{ validator: validateusername, trigger: "blur" }],
        password: [{ validator: validatepassword, trigger: "blur" }],
        email: [{ validator: validateemail, trigger: "blur" }],
        mobile: [{ validator: validatemobile, trigger: "blur" }],
      },
      addobj: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      //渲染提交参数 & 查询参数
      userobj: {
        pagenum: "1",
        pagesize: "2",
        query: "",
      },
      // searchId: "", id查询
      queryobj: [], //渲染存放的数组

      pagenum: 0, //页面个数
      total: 0, //总条数
      dialogVisible: false, //添加用户弹框
      dialogbj: false, //编辑弹窗
    };
  },
  methods: {
    handleSizeChange(val) {
      this.userobj.pagesize = val;
      this.query();
    },
    handleCurrentChange(val) {
      this.userobj.pagenum = val;
      this.query();
    },
    //添加用户
    addu() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.dialogVisible = false;
          let rst = await addusert_api(this.addobj);
          console.log(rst);
          if ((rst.meta.status = 201)) {
            if (rst.meta.msg == "用户名已存在") {
              this.$message.error(rst.meta.msg);
            } else {
              this.$message.success(rst.meta.msg);
              this.addobj = {};
            }
          } else {
            this.$message.error(rst.meta.msg);
          }
        } else {
          return false;
        }
      });
    },
    //查询用户
    async shuser() {
      // let rst = await search_api(this.searchId);
      // console.log(rst);
      // if (rst.meta.status == 200) {
      //   this.$message.success(rst.meta.msg);
      // } else {
      //   this.$message.error(rst.meta.msg);
      // }
      this.query();
    },
    //渲染数据
    async query() {
      let rst = await queryuser_api(this.userobj);
      this.queryobj = rst.data.users;
      this.total = rst.data.total;
      this.pagenum = rst.data.pagenum;
    },
    //状态激活
    async statech(row) {
      //id mg_state
      let rst = await state_api(row.id, row.mg_state);
      console.log(rst);
      if (rst.meta.status == 200) {
        this.$message.success(rst.meta.msg);
      } else {
        this.$message.error(rst.meta.msg);
      }
    },
    //编辑弹框
    bianjiquery(row) {
      this.dialogbj = true;
      let { id, email, mobile } = row;
      this.bianjiobj = { id, email, mobile };
    },
    //编辑提交
    async bianji() {
      this.dialogbj = false;
      let { email, mobile } = this.bianjiobj;
      let u = { email, mobile };
      let rst = await bianji_api(this.bianjiobj.id, u);
      console.log(rst);
      if (rst.meta.status == 200) {
        this.$message.success(rst.meta.msg);
        this.query();
      } else {
        this.$message.error(rst.meta.msg);
      }
    },
    //删除
    async del(row) {
      let rst = await del_api(row.id);
      console.log(row.id);
      console.log(rst);
    },
  },
  //
  mounted() {
    this.query();
  },
};
</script>

<style lang="less" scoped>
.headerr {
  width: 100%;
  justify-content: space-between;
  align-items: center;
  display: flex;
  .inheader {
    flex: 0 0 400px;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>